package cn.lgc;

import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.config.builder.CustomFile;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.fill.Column;
import com.baomidou.mybatisplus.generator.fill.Property;
import lombok.AllArgsConstructor;
import lombok.Getter;

import static java.lang.System.out;

public class CommonToolApplication {


    /**
     * 当前要生成的模块
     */
    private static final ModuleEnum TO_GENERATE_MODULE = ModuleEnum.OTHER;
    /**
     * 当前要生成的表
     */
    private static final String[] TO_GENERATE_TABLE_NAME = {"t_teacher"};

    public static void main(String[] args) {

        FastAutoGenerator
                .create("jdbc:mysql://localhost:3306" + TO_GENERATE_MODULE.getModulePath() + "?useSSL=false&serverTimezone=GMT%2B8&useUnicode=true&allowPublicKeyRetrieval=true&characterEncoding=utf-8", "root", "123456")
                .globalConfig(builder ->
                        builder
                                .outputDir(System.getProperty("user.dir") + TO_GENERATE_MODULE.getModuleService() + "/src/main/java") //输出到哪个目录
                                .author("陈立高")
                                .enableSwagger()
                                .dateType(DateType.TIME_PACK))//时间类型java时间
                .packageConfig(builder ->
                        builder
                                .parent(TO_GENERATE_MODULE.modulePackage) // 父包名
                                /*.moduleName("teacher")   // 父包模块名*/
                                .entity("domain")
                                .service("service")
                                .serviceImpl("service.impl")
                                .mapper("mapper")
                                .xml("mapper.xml")
                                .controller("controller")
                )
                .templateConfig(builder ->
                        builder
                                .entity("/templates/entity.java.vm")
                                .service("/templates/service.java.vm")
                                .serviceImpl("/templates/serviceImpl.java.vm")
                                .mapper("/templates/mapper.java.vm")
                                .xml("/templates/mapper.xml.vm")
                                .controller("/templates/controller.java.vm")
                ).injectionConfig(builder ->
                        builder
                                .beforeOutputFile((tableInfo, objectMap) -> {
                                    out.println("tableInfo: " + tableInfo.getEntityName() + " objectMap: " + objectMap.size());
                                })
                                //自定义模板
                                .customFile(new CustomFile.Builder().fileName("Query.java").filePath(System.getProperty("user.dir") + TO_GENERATE_MODULE.getModuleService() + "/src/main/java").enableFileOverride().templatePath("templates/query.java.vm").packageName(TO_GENERATE_MODULE.modulePackage + "/query").build())
                                .customFile(new CustomFile.Builder().fileName("Mapper.xml").filePath(System.getProperty("user.dir") + TO_GENERATE_MODULE.getModuleService() + "/src/main/resources").enableFileOverride().templatePath("templates/mapper.xml.vm").packageName(TO_GENERATE_MODULE.modulePackage + "/mapper").build())

                ).strategyConfig(builder ->
                        // 策略配置
                        builder
                                .enableCapitalMode()  // 开启大写命名
                                .addInclude(TO_GENERATE_TABLE_NAME)
                                .addTablePrefix("t_")// 需要加载的表
                ).strategyConfig(builder ->
                        builder
                                .entityBuilder()
                                .enableFileOverride()  // 覆盖已生成文件
                                .enableChainModel()
                                .enableLombok()
                                .enableTableFieldAnnotation()
                                .versionPropertyName("version")  // 乐观锁字段  生效要把乐观锁拦截器注入
                                .logicDeletePropertyName("deleted")
                                .addTableFills(new Column("create_time", FieldFill.INSERT))
                                .addTableFills(new Property("updateTime", FieldFill.INSERT_UPDATE))
                                .idType(IdType.ASSIGN_ID)   // 主键的ID类型
                ).strategyConfig(builder ->
                        builder
                                .controllerBuilder()
                                .enableFileOverride()  //  覆盖已生成文件	默认值:false
                                .enableHyphenStyle()
                                .enableRestStyle()
                ).strategyConfig(builder ->
                        builder.
                                serviceBuilder()
                                .formatServiceFileName("%sService")
                                .formatServiceImplFileName("%sServiceImpl")
                )/*.templateEngine(new FreemarkerTemplateEngine())*/
                .execute();

    }


    @AllArgsConstructor
    enum ModuleEnum {

        /**
         * module
         */

        OTHER("/java-926", "cn.lgc", ""),

        ;

        @Getter
        //数据库
        private String modulePath;
        @Getter
        //基础包路径
        private String modulePackage;
        @Getter
        //项目
        private String moduleService;

    }


}

